package com.wanger.swc.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.context.annotation.DependsOn;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @author longli
 */
@Data
@ApiModel
@DependsOn
@AllArgsConstructor
public class Result<T> implements Serializable {
    private static final long serialVersionUID = -28124576085555744L;

    @ApiModelProperty(notes = "响应码", example = "0")
    private final int code;

    @ApiModelProperty(notes = "响应消息", example = "提交成功")
    private final String msg;

    @ApiModelProperty(notes = "响应数据")
    private final T data;

    public static <T> Result<T> ok(T data) {
        return new Result<>(HttpStatus.OK.value(), "请求成功", data);
    }

    public static <Void> Result<Void> ok() {
        return new Result<>(HttpStatus.OK.value(), "请求成功", null);
    }

    public static <T> Result<T> error(T data) {
        return new Result<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "服务器内部错误！", data);
    }

    public static <T> Result<T> error(int code, String msg, T data) {
        return new Result<>(code, msg, data);
    }

    public static <T> Result<T> error(int code, String msg) {
        return new Result<>(code, msg, null);
    }

    public static <T> Result<T> error400() {
        return new Result<>(HttpStatus.BAD_REQUEST.value(), "参数错误！", null);
    }

    public static <T> Result<T> error401() {
        return new Result<>(HttpStatus.UNAUTHORIZED.value(), "用户未登录！", null);
    }

    public static <T> Result<T> error403() {
        return new Result<>(HttpStatus.FORBIDDEN.value(), "暂无权限！", null);
    }

    public static <T> Result<T> error500() {
        return new Result<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "服务器异常！", null);
    }
}
